#!/usr/bin/env ruby
#########################################################################

# Copyright (C) Flavio Piccinelli 2012 <flavio.picci@gmail.com>

#########################################################################

# hostCheck.rb
# This file is part of "AriaAperta".

# AriaAperta is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# AriaAperta is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with AriaAperta.  If not, see <http://www.gnu.org/licenses/>.

#########################################################################
#########################################################################


#########################################################################
# Required libraries
#########################################################################
require 'rubygems'
require 'sequel'
#########################################################################

HOSTNAME = `hostname`.chomp + "." + `dnsdomainname`.chomp
db = Sequel.sqlite("stats.db")

vars = 1
cpu = `ps aux | awk {'sum+=$3;print sum'} | tail -n 1`.chomp.to_f
mem = `free -m | grep Mem | awk {'print $4'}`.chomp.to_i
cpus = db[:hosts].filter(:name => HOSTNAME)

# puts cpu

10.times do |i|
	cpu_tmp = cpus.get(:"cpu#{i}")
	if !cpu_tmp.nil?
		cpu += cpu_tmp
		vars += 1
	end
end

bogos = cpus.get(:bogo)*(1 - (cpu/vars)/100)

# puts "#{cpu} è un #{cpu.class}"
# puts "#{bogos} è un #{bogos.class}"

puts "#{bogos}+#{mem}"
